package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.dao.MemberDao;
import com.xrit.sunnyday.dao.OrderMobileDao;
import com.xrit.sunnyday.dao.OrderSettingDao;
import com.xrit.sunnyday.dao.SetMealDao;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderMobileService;
import com.xrit.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

/**
 * 类名称: OrderMobileServiceImpl
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/29 下午 02:39
 * Version 1.0.0
 */
@Service(interfaceClass = OrderMobileService.class)
@Transactional
public class OrderMobileServiceImpl implements OrderMobileService {
    @Autowired
    private OrderMobileDao orderMobileDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Order submit(HashMap<String, String> orderInfo) throws SunnydayException {
        //日期转换
        Date orderDate = null;
            try {
                orderDate = DateUtils.parseString2Date(orderInfo.get("orderDate"), "yyyy-MM-dd");
            } catch (Exception e) {
                throw new SunnydayException("日期格式错误");
            }
        //先判断预约的时间存不存在
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (null==orderSetting){
            throw new SunnydayException("预约时间不存在");
        }
        //再判断已预约人数大于或者等于可预约人数
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            throw  new SunnydayException("当前预约时间已经预约满了");
        }
        //根据电话号码判断会员存不存在
      Member DBInMember= memberDao.findByTelePhone(orderInfo.get("telephone"));
        Integer memberId = null;
        //拿到会员进行判断 会员不存在就进行添加
        Member member = new Member();
        if (null==DBInMember){
            member.setName(orderInfo.get("name"));
            member.setIdCard(orderInfo.get("idCard"));
            member.setSex(orderInfo.get("sex"));
            member.setPhoneNumber(orderInfo.get("telephone"));
            member.setRegTime(new Date());
            memberDao.add(member);
            memberId = member.getId();
        }else {
            memberId = DBInMember.getId();
        }
        //根据订单的套餐,会员,日期 查询
        Integer setMealId = Integer.valueOf(orderInfo.get("setmealId"));
        Order order = new Order();
        order.setOrderDate(orderDate);
        order.setMemberId(memberId);
        order.setSetmealId(setMealId);
        //先用他代替订单的id
        Order DBInorder= orderMobileDao.findByOrder(order);
        //需要进行测试先注释掉
        //判断订单是否存在
        if (null!=DBInorder){
            throw new SunnydayException("您已经预约过了");
        }
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
//        //不存在就进行添加
        orderMobileDao.add(order);
//        //最后预约设置的修改 预约人数加一
        orderSettingDao.addReservations1(orderDate);
        return order;
    }

    @Override
    public HashMap<String, String> findById(Integer id) {
       return orderMobileDao.findById(id);
    }
}
